package com.hihuzi.test.Demo;

import java.io.*;
import java.util.ArrayList;

public class Person02 implements Serializable {
    private String name;
    private int age;

    public Person02(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return name + "(" + age + ")";
    }

    private Object writeReplace() throws ObjectStreamException {
        ArrayList<Object> list = new ArrayList<>();
        list.add(name);
        list.add(age);
        return list;
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test.buf"))) {
            Person02 p = new Person02("lala", 33);
            oos.writeObject(p);
            oos.close();
        }
        Object obj = null;
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.buf"))) {
            obj = (ArrayList) ois.readObject();
            System.out.println((obj).toString());

        }
    }

}

